package com.hanlink.dsp.ui.widget;

import android.content.Context;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.edge.pbxdsp.R;


//import butterknife.BindView;
//import butterknife.BindViews;
//import butterknife.ButterKnife;
//import butterknife.OnCheckedChanged;

public class Summing2InputView
        extends LinearLayout implements CompoundButton.OnCheckedChangeListener {

    private CheckBox cb1,cb2,cb3,cb4;

    //@BindViews({R.id.cb1, R.id.cb2, R.id.cb3, R.id.cb4})
    CheckBox[] mCheckBoxs;
    private OnCheckedChangeListener mOnCheckedChangeListener;
    private int mTag;
    //@BindView(R.id.text1)
    CheckBox mText1;
    //@BindView(R.id.text2)
    CheckBox mText2;

    public Summing2InputView(Context context) {
        this(context, null, 0);
    }

    public Summing2InputView(Context context, @Nullable AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public Summing2InputView(Context context, @Nullable AttributeSet attributeSet, int style) {
        super(context, attributeSet, style);
        init();
    }

    private void init() {
       View view =  View.inflate(getContext(), R.layout.summing2_input_view, this);
      //  ButterKnife.bind(this, this);
        cb1 = view.findViewById(R.id.cb1);
        cb2 = view.findViewById(R.id.cb2);
        cb3 = view.findViewById(R.id.cb3);
        cb4 = view.findViewById(R.id.cb4);

        mCheckBoxs = new CheckBox[]{cb1,cb2,cb3,cb4};
        mText1 = view.findViewById(R.id.text1);
        mText2 = view.findViewById(R.id.text2);

        for(CheckBox cb : mCheckBoxs){
            cb.setOnCheckedChangeListener(this);
        }

        mText1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mCheckBoxs[0].setChecked(isChecked);
                boolean isNotify = mText1.getTag() == null ? true : (boolean) mText1.getTag();
                if (!isNotify) {
                    mText1.setTag(true);
                    return;
                }
                if (mOnCheckedChangeListener != null) {
                    mOnCheckedChangeListener.onCheckedChanged(mTag, true, isChecked);
                }
            }
        });

        mText2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mCheckBoxs[2].setChecked(isChecked);
                boolean isNotify = mText2.getTag() == null ? true : (boolean) mText2.getTag();
                if (!isNotify) {
                    mText2.setTag(true);
                    return;
                }
                if (mOnCheckedChangeListener != null) {
                    mOnCheckedChangeListener.onCheckedChanged(mTag, false, isChecked);
                }
            }
        });
    }

   /* @OnCheckedChanged({R.id.cb1, R.id.cb2, R.id.cb3, R.id.cb4})
    public void checkChanged(CompoundButton button, boolean isChecked) {
        switch (button.getId()) {
            case R.id.cb1:
                this.mCheckBoxs[1].setChecked(isChecked);
                this.mText1.setChecked(isChecked);
                return;
            case R.id.cb2:
                this.mCheckBoxs[0].setChecked(isChecked);
                this.mText1.setChecked(isChecked);
                return;
            case R.id.cb3:
                this.mCheckBoxs[3].setChecked(isChecked);
                this.mText2.setChecked(isChecked);
                return;
            case R.id.cb4:
                this.mCheckBoxs[2].setChecked(isChecked);
                this.mText2.setChecked(isChecked);
                return;
        }
    }*/

   /* @OnCheckedChanged({R.id.text1, R.id.text2})
    public void onTextCheckedChanged(CompoundButton button, boolean isChecked) {
        switch (button.getId()) {
            case R.id.text1: {
                this.mCheckBoxs[0].setChecked(isChecked);
                boolean isNotify = mText1.getTag() == null ? true : (boolean) mText1.getTag();
                if (!isNotify) {
                    mText1.setTag(true);
                    return;
                }
                if (mOnCheckedChangeListener != null) {
                    mOnCheckedChangeListener.onCheckedChanged(mTag, true, isChecked);
                }
                break;
            }
            case R.id.text2: {
                this.mCheckBoxs[2].setChecked(isChecked);
                boolean isNotify = mText2.getTag() == null ? true : (boolean) mText2.getTag();
                if (!isNotify) {
                    mText2.setTag(true);
                    return;
                }
                if (mOnCheckedChangeListener != null) {
                    mOnCheckedChangeListener.onCheckedChanged(mTag, false, isChecked);
                }
                break;
            }
        }
    }*/

    public void setCheckedFirst(boolean isChecked) {
        if (mText1.isChecked() != isChecked) {
            this.mText1.setTag(false);
            this.mText1.setChecked(isChecked);
        }
    }

    public void setCheckedSecond(boolean isChecked) {
        if (mText2.isChecked() != isChecked) {
            this.mText2.setTag(false);
            this.mText2.setChecked(isChecked);
        }
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener paramOnCheckedChangeListener) {
        this.mOnCheckedChangeListener = paramOnCheckedChangeListener;
    }

    public void setup(boolean firstChecked, boolean secondChecked, String text1, String text2, int tag) {
        this.mTag = tag;
        this.mText1.setText(text1);
        this.mText2.setText(text2);
        setCheckedFirst(firstChecked);
        setCheckedSecond(secondChecked);
//        mText1.setTag(true);
//        mText2.setTag(true);
//        this.mText1.setChecked(firstChecked);
//        this.mText2.setChecked(secondChecked);

    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        for (CheckBox checkBox : mCheckBoxs) {
            checkBox.setEnabled(enabled);
        }
        mText1.setEnabled(enabled);
        mText2.setEnabled(enabled);

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.cb1:
                this.mCheckBoxs[1].setChecked(isChecked);
                this.mText1.setChecked(isChecked);
                return;
            case R.id.cb2:
                this.mCheckBoxs[0].setChecked(isChecked);
                this.mText1.setChecked(isChecked);
                return;
            case R.id.cb3:
                this.mCheckBoxs[3].setChecked(isChecked);
                this.mText2.setChecked(isChecked);
                return;
            case R.id.cb4:
                this.mCheckBoxs[2].setChecked(isChecked);
                this.mText2.setChecked(isChecked);
                return;
        }
    }

    public interface OnCheckedChangeListener {
        void onCheckedChanged(int tag, boolean isFirst, boolean isChecked);
    }

}
